<?php
/**
 *  NevuxBB - Free Forum Software
 *  Copyright (C) 2008, 2010 NevuxBB (No one specific author)This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Login System
 * File: login.php
 * Purpose: Allow the user to login or recover their account
 * Todo: Finish writing it
 *
 */
require('system/files/global.php');

// start the initalisation of the login page
$core_template_system->setvar('page_title', 'Login');

// show the header loaded
$core_template_system->pparse();

// include the language file for this page
$core_lang_system->reset();
$core_lang_system->add_file('login');

// build the menu
$core_template_system->setvar('breadcrumb', $core_nav_system->generateNav(array('settings' => array('level_down' => false,
																									'main_class' => 'menu'),
																				'information' => array('home' => array('data' => array(1 => 'Home',
																																	   2 => 1)),
																									   'login' => array('data' => array(1 => 'Login',
																																		2 => 4))))));

// start the page
if(CoreUserSystem::isLoggedIn()) {
	// user is already logged in
	$core_template_system->setvar('error_message', $core_lang_system->get_var('ALREADY_LOGGED_IN', 3));
	$core_template_system->setvar('error', $core_lang_system->get_var('ERROR', 4));
	$core_template_system->setvar('back_url', 'index.php');
	
	// render the page
	$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/error.html');
	$core_template_system->pparse();
} else {
	if(isset($_POST['nev_form_submit'])) {
		// form submitted
		if(isset($_POST['nev_form_bot_box'])) {
			// bot hack attempt
			$core_template_system->setvar('error_message', $core_lang_system->get_var('HACK_ERROR', 4));
			$core_template_system->setvar('error', $core_lang_system->get_var('ERROR', 4));
			$core_template_system->setvar('back_url', 'login.php');
			
			// log this attempt
			SystemSettings::addNewAttempt($_SERVER['REMOTE_ADDR'], $_SERVER['REQUEST_URI'], 'hack', array('username' => $_POST['nev_form_user'], 'password' => $_POST['nev_form_pass']));
			
			// render the page
			$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/error.html');
			$core_template_system->pparse();
		} else {
			// real request, process it
			if($core_base_system->login($_POST['nev_form_user'], $_POST['nev_form_pass'])) {
				// successful
				$core_template_system->setvar('click_redirection_message', $core_lang_system->get_var('CLICK_REDIRECTION', 4));
				$core_template_system->setvar('redirection_message', $core_lang_system->get_var('CURRENT_REDIRECTION', 4));
				$core_template_system->setvar('url_to_go_to', 'index.php');
				$core_template_system->setvar('redirection', $core_lang_system->get_var('REDIRECTION', 4));
				
				// before we start redirecting, set last login stuff for read/unread topics
				if(empty($_SESSION['User']['user_la1'])) {
					// has never logged in before, set current timestamp as "last visit"
					// current time
					$now_time = time();
					
					// update this in the database
					Db_Abstraction::query("UPDATE `nbb_users` SET `user_la1` = '$now_time' WHERE `user_id` = '" . $core_base_system->getUserId() . "'");
					
					// update current session too
					$_SESSION['User']['user_la1'] = $now_time;
				} else {
					// has logged in before
					if(empty($_SESSION['User']['user_la2'])) {
						// has never logged in a second time
						// update this with current timestamp
						Db_Abstraction::query("UPDATE `nbb_users` SET `user_la2` = '" . time() . "' WHERE `user_id` = '" . $core_base_system->getUserId() . "'");
					} else {
						// user_la2 needs to become user_la1 to make way for a new "last visit"
						Db_Abstraction::query("UPDATE `nbb_users` SET `user_la1` = `user_la2`, `user_la2` = '" . time() . "' WHERE `user_id` = '" . $core_base_system->getUserId() . "'");
						
						// make this change visible right now to the current session
						$_SESSION['User']['user_la1'] = $_SESSION['User']['user_la2'];
					}
				}
				
				// render the page
				$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/redirection.html');
				$core_template_system->pparse();
			} else {
				// unsuccessful
				$core_template_system->setvar('login', $core_lang_system->get_var('LOGIN', 4));
				$core_template_system->setvar('username', $core_lang_system->get_var('USERNAME', 4));
				$core_template_system->setvar('password', $core_lang_system->get_var('PASSWORD', 4));
				$core_template_system->setvar('incorrect_login', true);
				
				// render the page
				$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/login.html');
				$core_template_system->pparse();
			}
		}
	} else {
		// show the form
		$core_template_system->setvar('login', $core_lang_system->get_var('LOGIN', 4));
		$core_template_system->setvar('username', $core_lang_system->get_var('USERNAME', 4));
		$core_template_system->setvar('password', $core_lang_system->get_var('PASSWORD', 4));
		
		
		
		// render the page
		$core_template_system->newTemplate(BASE_PATH . '/templates/' . ForumSettings::getCurrentTemplate() . '/login.html');
		$core_template_system->pparse();
	}
}

require('system/files/footer.php');
?>